<template>
  <div>
    <h1>当前车次为:<b style="color: red">{{ transportTripData.name }}</b></h1>

    <el-form v-model="tripsForm" label-width="80px" class="demo-form-inline" :inline="true">
      <el-form-item label="车辆安排:">
        <el-select v-model="tripsForm.truckId" multiple size="medium" placeholder="请选择车辆">
          <el-option
            v-for="item in truckData"
            :key="item.id"
            :label="item.brand"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="司机安排:">
        <el-select v-model="tripsForm.driverId" multiple size="medium" placeholder="请选择司机">
          <el-option
            v-for="item in driverData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="add">添加</el-button>
    </el-form>
  </div>
</template>

<script>
import transportTrips from '@/api/base/transportTrips/index'
import truck from '@/api/base/truck/index'
import user from '@/api/base/globalUser/index'
import driver from '@/api/base/driver/index'

export default {
  name: 'driverAndTruck',
  data() {
    return {
      tripsForm: {
        truckId: [],
        driverId: []
      },
      tripsId: '',
      transportTripsData: [],
      truckData: [],
      fleetData: [],
      userData: [],
      driverData: [],
      transportTripData: {}
    }
  }, methods: {
    add() {
      if (this.tripsForm.truckId.length !== this.tripsForm.driverId.length) {
        this.$message({
          showClose: true,
          message: '司机或者车次数量不匹配',
          type: 'warning'

        })
        return
      }
      let data = []
      let truckIds = this.tripsForm.truckId
      let driverId = this.tripsForm.driverId
      for (let i = 0; i < truckIds.length; i++) {
        let a = {
          transportTripsId: this.tripsId,
          truckId: truckIds[i],
          userId: driverId[i]
        }
        data.push(a)
      }
      transportTrips.add(this.tripsId, data).then(data => {
        this.$message({
          showClose: true,
          message: '分配成功!',
          type: 'success'
        })
      })
      this.$router.push({ path: '/transport-trips-index' })
    },
    findTransportTripsById() {
      transportTrips.findTransportTripsById(this.tripsId).then(data => {
        this.transportTripData = data.data
      })
    },
    findAllTruckDriverTransportTrips() {
      transportTrips.findAllTruckDriverTransportTrips(this.tripsId).then(data => {
        this.transportTripsData = data.data;
        console.log("transportTripsData:",data.data)
        let transportTripsData=this.transportTripsData;
        for (let i=0;i<transportTripsData.length;i++){
          this.tripsForm.driverId.push(transportTripsData[i].userId);
          this.tripsForm.truckId.push(transportTripsData[i].truckId);
        }
      })
    },
    //获取车辆数据
    getAllTruck() {
      truck.getAllTruck().then(data => {
        this.truckData = data.data
      })
    },
    //获取车队数据
    getAllFleet() {
      truck.getAllFleet().then(data => {
        this.fleetData = data.data
      })
    },
    //获取用户数据
    getGlobalUserData() {
      user.queryAll().then(data => {
        this.userData = data.data
        this.findAllDriver()
      })
    },
    //获取司机数据
    findAllDriver() {
      driver.findAll().then(data => {
        let userData = this.userData
        let driver = data.data
        for (let j = 0; j < driver.length; j++) {
          for (let i = 0; i < userData.length; i++) {
            if (driver[j].userId === userData[i].id) {
              userData[i].driverId = driver[j].id
              userData[i].fleetId = driver[j].fleetId
              this.driverData.push(userData[i])
            }
          }
        }
      })
    }
  }, mounted() {
    this.tripsId = this.$route.params.id;
    console.log("tripsId:",this.tripsId)
    this.findAllTruckDriverTransportTrips()
    this.getAllTruck()
    this.getAllFleet()
    this.getGlobalUserData()
    this.findTransportTripsById()
  }
}
</script>

<style scoped>

</style>
